<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Banner extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by='created_date', $sort_order='asc', $current_page=0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					//echo '<pre>';print_r($search_by);exit;
					
					}
				}
			$results_per_page = 3;
			
			$url = CURL_URL;
			$data = array('www-command'=>'banners-all_banners', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order,'search_by'=>$search_by,'search_by_value'=>$search_by_value);
			$response= $this->get_banner_list($data);
			
			if($response->message !== 'No Banners'){
			//echo '<pre>';print_r($response);exit;
			
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/banner/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows'] = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['banners_all_banners'] = $response;
			$data['banner'] = '';
			
			}else{
				$data['banner'] = 1;
				$data['banners_all_banners'] = "";
			}
			$data['sort_order'] = $sort_order;
			$data['order_by'] = $order_by;
			$data['query_id'] = $query_id;
			$data['banner_fields'] = array('banner_name'=>'Name', 'image_name'=>'Image', 'sequence'=>'Sequence', 'featured'=>'Featured', 'event_block'=>'Events Block', 'classifieds'=>'Above Classifieds', 'status'=>'Status', 'created_date'=>'Date');
			//echo '<pre>';print_r($data);exit;
			$this->load->view('admin/banner', $data);
 		}
		
		function get_banner_list($data){
				$url = CURL_URL;
				$string = http_build_query($data);
				//echo '<pre>';print_r($data);exit;
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$response = json_decode(curl_exec($ch));
				//echo '<pre>';print_r($response);exit;
				curl_close($ch);
				return $response;
			}
		
		function search_by(){
				$search = $this->input->post('search');	
				
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/banner/index/'.$query_id);
			
		}
		
		/* function create_banner_page(){
			$this->load->view('admin/create_banner_page');
		} */
		
		function create_banner(){
			$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('view', 'View', 'required|callback_select');
				$this->form_validation->set_rules('sequence', 'Sequence', 'required');
				$this->form_validation->set_rules('image', 'Image','callback_image');
				$this->form_validation->set_rules('url', 'URL', 'required|callback_valid_url_format');
				
				if($this->form_validation->run() == FALSE){
						$this->load->view('admin/create_banner_page');
				}else{
				
					if($this->input->post('featured') == 'on'){
						$featured = 1;
					}else{
							$featured = 0;
					}
					if($this->input->post('event') == 'on'){
						$event = 1;
					}else{
							$event = 0;
					}if($this->input->post('classified') == 'on'){
						$classified = 1;
					}else{
							$classified = 0;
					}
					$filesupload = array('tmp_name'=>$_FILES['image']['tmp_name'],'name'=>$_FILES['image']['name']);	
					$data = array('www-command'=>'banners-create_banner', 
					'banner_name' => $this->input->post('name'),
					'banner_view' => $this->input->post('view'),
					'sequence' => $this->input->post('sequence'),
					'banner_url' => $this->input->post('url'),
					'banner_image' => $filesupload,
					'featured' => $featured,
					'event_block' => $event,
					'classifieds' => $classified
					);
					//echo '<pre>';print_r($data);exit;
					$response= $this->get_banner_list($data);
					//echo '<pre>';print_r($response);exit;
					redirect('admin/banner/index/');
				}
			}
			
			function image(){
				//echo '<pre>';print_r($_FILES);exit;
				if($_FILES['image']['name'] == ''){
					//echo '<pre>';print_r($_FILES);exit;
					$this->form_validation->set_message('image','Image field is required');
					return false;
				}else{
					return true;
				}
			}
			
			function valid_url_format($url){
			if($url == ''){
					return true;
				}
			$pattern = '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
			if(!preg_match($pattern, $url)){
				$this->form_validation->set_message('valid_url_format', 'The URL you entered is not accessible.');
				return false;
				}
				return true;
			}
			
			function select($select)
				{
				 	if($select == 'Select View')
					{ 
					  $this->form_validation->set_message('select', 'Please select view.');
					  return FALSE;
					}
				 return TRUE;
				 
				}
				
				function edit_banner(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('name', 'Name', 'required');
				$this->form_validation->set_rules('view', 'View', 'required|callback_select');
				$this->form_validation->set_rules('sequence', 'Sequence', 'required');
				//$this->form_validation->set_rules('image', 'Image','callback_image');
				$this->form_validation->set_rules('url', 'URL', 'required|callback_valid_url_format');
		
				if($this->form_validation->run() == FALSE){
					if($this->uri->segment(4) > 0){
					$_SESSION['banner_id'] = $this->uri->segment(4);
				}
					$data = array('www-command'=>'banners-get_banner', 
					'banner_id' => $_SESSION['banner_id']
					);
					$response = $this->get_banner_list($data);
					//echo '<pre>';print_r($response);exit;
					$response = (array) $response;
					foreach($response as $value){
						$value = $value;
					}
					$data['name'] = $value[0]->banner_name;
					$data['view'] = $value[0]->banner_view;
					$data['sequence'] = $value[0]->sequence;
					$data['image_name'] = $value[0]->image_name;
					$data['image'] = $value[0]->banner_image;
					$data['url'] = $value[0]->banner_url;
					$data['featured'] = $value[0]->featured;
					$data['event'] = $value[0]->event_block;
					$data['classified'] = $value[0]->classifieds;
					$data['status'] = $value[0]->status;
					$this->load->view('admin/edit_banner_page', $data);
					
				}else{
				
					if($this->input->post('featured') == 'on'){
						$featured = 1;
					}else{
							$featured = 0;
					}
					if($this->input->post('event') == 'on'){
						$event = 1;
					}else{
							$event = 0;
					}if($this->input->post('classified') == 'on'){
						$classified = 1;
					}else{
							$classified = 0;
					}if($this->input->post('status') == 'on'){
						$status = 1;
					}else{
							$status = 0;
					}
					
					$filesupload = array('tmp_name'=>$_FILES['image']['tmp_name'],'name'=>$_FILES['image']['name']);	
					$data = array('www-command'=>'banners-edit_banner', 
					'banner_name' => $this->input->post('name'),
					'banner_view' => $this->input->post('view'),
					'sequence' => $this->input->post('sequence'),
					'banner_url' => $this->input->post('url'),
					'banner_image' => $filesupload,
					'featured' => $featured,
					'event_block' => $event,
					'classifieds' => $classified,
					'status' => $status,
					'banner_id' => $_SESSION['banner_id']
					);
					//echo '<pre>';print_r($data);exit;
					$response = $this->get_banner_list($data);
					//echo '<pre>';print_r($response);exit;
					redirect('admin/banner/index/');
				}
			}
			
			function delete_banner(){
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'banners-delete_banner','status'=>$status, 'banner_id'=>$this->uri->segment(4));
				$response = $this->get_banner_list($data);
				redirect('admin/banner/index/');
			}
}




